package com.geekbang.homework.WeekTwoHomework;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class TwoSum {
    public static void main(String[] args) {
        int[] nums = {1, 8, 11, 15};
//        int[] result = sTwoSum(nums, 9);
        int[] result = sTwoSumT(nums, 9);

        System.out.println(Arrays.toString(result));
    }

    public static int[] sTwoSum(int[] nums, int target) {
        Map<Integer, Integer> hashTable = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (hashTable.containsKey(target - nums[i])) {
                return new int[]{hashTable.get(target - nums[i]), i};
            }
            hashTable.put(nums[i],i);
        }
        return new int[0];
    }

    public static int[] sTwoSumT(int[] nums, int target){
        int []  a = new int[2];
        for (int i = 0; i < nums.length; i++){
            for(int j = i +1; j < nums.length; j++){
                if(nums[i] + nums[j] == target){
                    a[0] = i;
                    a[1] = j;
                    return a;
                }
            }
        }
        return new int [0];
    }


}
